<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/

/**
 * Get all posts
 * @returns array
 * @return array of zero or more subscriptions, or NULL on failure
 * @param tagid interger tag id 
 */
function conversations_userapi_getposts_bytagid($args)
{
    extract($args);

    // Optional arguments
    if (!isset($startnum)) {$startnum = 1;}
    if (!isset($numitems)) {$numitems = -1;}

    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $cposts = $xartable['cposts'];
    $clook  = $xartable['clook'];
    $bind   = array();
   
    $query = "SELECT $cposts.xar_postid, $cposts.xar_timestamp, $cposts.xar_uid, "
            . "$cposts.xar_text, $cposts.xar_count, $cposts.xar_options, $clook.xar_tagid"
            . " FROM $cposts "
            . " LEFT JOIN $clook "
            . " ON $cposts.xar_postid = $clook.xar_postid "
            . " WHERE $clook.xar_tagid = ? ";
            $bind[] = (int)$tagid;
            
    if(!empty($from)){
       $query .= " AND $cposts.xar_timestamp >= ? - 10 ";
       $bind[] = $from;
    }       
            
    $query .= " ORDER by $cposts.xar_postid";

    $where = array();
    
    $result =& $dbconn->SelectLimit($query, $numitems, $startnum-1, $bind);
    if (!$result) return;

    $posts = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($postid, $timestamp, $uid, $text, $status, $count, $options) = $result->fields;
        
        $posts[] = array(
                'postid'    => $postid,
                'timestamp' => $timestamp,
                'uid'       => $uid,
                'text'      => xarVarPrepForDisplay($text), //Prep it here
                'status'    => $status,
                'count'     => $count,
                //'options'   => unserialize($options), // Unserialize it here
                'tagid'     => $tagid // Might as well have it in the array.  No telling when it might be useful
        );
    }
    $result->Close();
    return $posts;
}
?>